<?php
class Sss_Controller_Shepherd extends Sss_Controller
{
	public function init() 
	{
		parent::init();
		$handle =$this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$commonTable = Sss_Service_Locator::getInstance()->getService('Sss_Table_Abstract');
		$commonTable->setCurrentHandleName($handle);
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $handle);
		$instanceSession = $this->getValueRequest(Sss_Ultility_SessionName::INSTANCE_NAME, Sss_Ultility_CommonViewParamater::GET_SESSION);
		$instanceName = '';
		if (is_null($instanceSession)) {
			$instanceTable = new Application_Model_Mapper_Instance();
			$instance = $instanceTable->getInstanceByHandle($handle);
			if (!is_null($instance)) {
				$instanceName =  $instance->instancename;
				$this->getValueRequest(Sss_Ultility_SessionName::INSTANCE_NAME, $instanceName);
			}
		} else {
			$instanceName = $instanceSession;
		}

		if (!is_null($userInformation->accountid)) {
			$arrSavedCharts = $this->loadSaveCharts(true);
			$this->view->assign('chartmenu', count($arrSavedCharts) > 0 ? implode(',', $arrSavedCharts) : '');
		}
		$this->view->assign(Sss_Ultility_SessionName::INSTANCE_NAME, $instanceName);
	}

	public function loadSaveCharts($saveChart = null)
	{
		$arrSavedCharts = array();
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$chartSessionTable = new Shepherd_Model_Mapper_ChartSession();
		if (!is_null($userInformation->accountid)) {
			if($saveChart){
				$results =  $chartSessionTable->getChartSessionByAccount(null, 2);
			}else{
				$results = $chartSessionTable->getChartSessionByAccount(null, null);
			}
			if (count($results) > 0) {
				foreach ($results as $rs) {
					if (!in_array($rs['chart'], $arrSavedCharts)) {
						$arrSavedCharts[] = $rs['chart'];
					}
				}
			}
		}
		return $arrSavedCharts;
	}

	public function checkInstance()
	{
		//get information user and display fullname to view
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$this->view->assign(Sss_Ultility_CommonViewParamater::FULLNAME, $userInformation->fullname);
		$this->view->assign('username', $userInformation->username);
		$instanceTable = new Application_Model_Mapper_Instance();

		//get session list role 
		$sessionRoleList  = new Zend_Session_Namespace(Sss_Ultility_SessionName::ROLE_LIST_SESSION . $this->getCurrentHandle());
		$this->view->assign(Sss_Ultility_CommonViewParamater::LIST_ROLE, $sessionRoleList->roleList);

		//get handle name
		$handle =$this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$handleSession = new Zend_Session_Namespace(Sss_Ultility_SessionName::HANDLE_SESSION . $this->getCurrentHandle());
		$handleSession->handle = $handle;
		$defaultNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::SHEPHERD_SYSTEM . $this->getCurrentHandle());
		//get instance for check when not found
		$instance = $instanceTable->getInstanceByHandle($handle);
		$accountTable = new Shepherd_Model_Mapper_Account();
		$sessionStartLogin = new Zend_Session_Namespace(Sss_Ultility_SessionName::START_LOGIN_SESSION . $this->getCurrentHandle());

		$account = true;
		if($userInformation->username) {
			$account = $accountTable->getUser($userInformation->username, $instance->instanceid);
		} elseif ($sessionStartLogin->startLogin  == Sss_Ultility_Setting::END_LOGIN) {

		} elseif ($sessionStartLogin->startLogin == null) {
			$sessionStartLogin->startLogin = Sss_Ultility_Setting::START_LOGIN;  
		}

		if($instance == false || $instance == null || $account == false || $instance->connected == 0 || $sessionStartLogin->startLogin == Sss_Ultility_Setting::START_LOGIN){
			$sessionStartLogin->startLogin  = Sss_Ultility_Setting::END_LOGIN;
			if ($this->view->controllerName != 'user' && $this->view->actionName != 'login') {
				return $this->_redirect("/$handle/user/login");
			}
		} else { 
			$defaultNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::SHEPHERD_SYSTEM . $this->getCurrentHandle());
			$defaultNamespace->dbShepherd = $instance->instanceid;
			$defaultNamespace->handle = $instance->handle;
			$defaultNamespace->stocklocationId = $instance->stocklocation;
			return $instance;
		}
	}

	public function listRole()
	{
		$sessionRoleList  = new Zend_Session_Namespace(Sss_Ultility_SessionName::ROLE_LIST_SESSION . $this->getCurrentHandle());
		$listPermissionByRole =  (array)$sessionRoleList->roleList;
		return   $listPermissionByRole;
	}

	public function clearSession()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$userInformation->setExpirationSeconds(Sss_Ultility_Setting::EXPIRATION);
		$defaultNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::SHEPHERD_SYSTEM . $this->getCurrentHandle());
		$defaultNamespace->setExpirationSeconds(Sss_Ultility_Setting::EXPIRATION);
		$defaultOvisNamespace  = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $this->getCurrentHandle());
		$defaultOvisNamespace->setExpirationSeconds(Sss_Ultility_Setting::EXPIRATION);
		$sessionStartLogin = new Zend_Session_Namespace(Sss_Ultility_SessionName::START_LOGIN_SESSION . $this->getCurrentHandle());
		$sessionStartLogin->setExpirationSeconds(Sss_Ultility_Setting::EXPIRATION);
		$sessionRoleList  = new Zend_Session_Namespace(Sss_Ultility_SessionName::ROLE_LIST_SESSION . $this->getCurrentHandle());
		$sessionRoleList->setExpirationSeconds(Sss_Ultility_Setting::EXPIRATION);
		$roomInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ROOM_INFORMATION_SESSION . $this->getCurrentHandle());
		$roomInformation->setExpirationSeconds(Sss_Ultility_Setting::EXPIRATION);
		$this->getValueRequest(Sss_Ultility_SessionName::INSTANCE_NAME, null);
		$autoLogin = new Zend_Session_Namespace(Sss_Ultility_SessionName::SHEPHERD_AUTO_LOGIN_SESSION);
		$autoLogin->setExpirationSeconds(Sss_Ultility_Setting::EXPIRATION);
		$this->getValueRequest(Sss_Ultility_SessionName::SHEPHERD_AUTO_LOGIN_SESSION, null);
	}
}